{extend name="base"/}
{block name="resources"}
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">操作提示</h2>
		<ul class="layui-colla-content layui-show">
			<li>当前页面针对入驻申请进行管理，可以针入驻申请进行审核通过，审核拒绝操作。</li>
		</ul>
	</div>
</div>

<div class="ns-screen layui-collapse" lay-filter="selection_panel">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">筛选</h2>

		<form class="layui-colla-content layui-form layui-show">
			<!--<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">主营行业：</label>
					<div class="layui-input-inline">
						<select name="category_id" lay-filter="category_id">
							<option value="">请选择主营行业</option>
							{volist name="$shop_category_list" id="category"}
							<option value="{$category.category_id}">{$category.category_name}</option>
							{/volist}
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">开店套餐：</label>
					<div class="layui-input-inline">
						<select name="group_id" lay-filter="group_id">
							<option value="">请选择开店套餐</option>
							{volist name="$shop_group_list" id="group"}
							<option value="{$group.group_id}">{$group.group_name}</option>
							{/volist}
						</select>
					</div>
				</div>
			</div>-->
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">网点名称：</label>
					<div class="layui-input-inline">
						<input type="text" id="search_text" name="search_text" placeholder="网点名称" autocomplete="off" class="layui-input">
					</div>
				</div>
				
				<div class="layui-inline">
					<label class="layui-form-label">网点系统编号：</label>
					<div class="layui-input-inline">
						<input type="text" id="apply_no" name="apply_no" placeholder="网点系统账号" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">网点分类：</label>
					<div class="layui-input-inline">
						<select name="category_id" lay-filter="category_id">
							<option value="">网点分类</option>
							{volist name="$shop_category_list" id="category"}
							<option value="{$category.category_id}">{$category.category_name}</option>
							{/volist}
						</select>
					</div>
				</div>
			</div>
			
			<!--<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">申请时间：</label>
					<div class="layui-input-inline">
					    <input type="text" class="layui-input" name="start_time"  id="start_time" autocomplete="off" placeholder="开始时间" readonly>
						<i class="ns-calendar"></i>
					</div>
					<div class="layui-form-mid">-</div>
					<div class="layui-input-inline">
					    <input type="text" class="layui-input" name="end_time" id="end_time" autocomplete="off" placeholder="结束时间" readonly>
						<i class="ns-calendar"></i>
					</div>
				</div>
			</div>-->
			<input type="hidden" name="apply_state" />
			<div class="ns-form-row">
				<button class="layui-btn ns-bg-color" lay-submit lay-filter="search">筛选</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</form>

	</div>
</div>

<div class="layui-tab ns-table-tab" lay-filter="apply_list_tab">
	<ul class="layui-tab-title">
		<li class="layui-this" lay-id="">全部</li>
		<li lay-id="1" data-type="apply_state">待审核</li>
		<li lay-id="-1" data-type="apply_state">审核失败</li>
<!--		<li lay-id="4" data-type="apply_state">待提交财务凭证</li>-->
<!--		<li lay-id="2" data-type="apply_state">待财务审核</li>-->
		<li lay-id="3" data-type="apply_state">审核成功</li>
<!--		<li lay-id="-2" data-type="apply_state">财务审核失败</li>-->
	</ul>
	<div class="layui-tab-content">
		{if $is_addon_city == 1}
		<table id="apply_website_list" lay-filter="apply_website_list"></table>
		{else/}
		<table id="apply_list" lay-filter="apply_list"></table>
		{/if}
	</div>
</div>

<input type="hidden" id="is_addon_city" value="{$is_addon_city}" />

<!-- 工具栏操作 -->
<script type="text/html" id="action">
	<div class="ns-table-btn">
		{{#  if(d.apply_state == 1){ }}
		<a class="layui-btn" href="javascript:orderDelivery('{{d.apply_id}}')">审核</a>
<!--		<a class="layui-btn" lay-event="apply_pass">审核</a>-->
<!--		<a class="layui-btn" id="apply_message" lay-event="apply_refuse">拒绝申请</a>-->
		<a class="layui-btn" lay-event="apply_detail">认证信息</a>
		{{#  } }}
		{{#  if(d.apply_state == -1){ }}
		<a class="layui-btn" lay-event="apply_detail">认证信息</a>
		{{#  } }}
		<!--{{#  if(d.apply_state == 2){ }}
			{{#  if(d.paying_money_certificate){ }}
			<a class="layui-btn" lay-event="edit_apply">支付信息</a>
			{{# } }}
		<a class="layui-btn" lay-event="apply_detail">认证信息</a>
		{{#  } }}-->
		{{#  if(d.apply_state == 3){ }}
		<a class="layui-btn" lay-event="apply_detail">认证信息</a>
<!--		<a class="layui-btn" lay-event="edit_apply">支付信息</a>-->
		{{#  } }}
		<!--{{#  if(d.apply_state == -2){ }}
		<a class="layui-btn" lay-event="edit_apply">财务拒绝</a>
		<a class="layui-btn" lay-event="apply_detail">认证信息</a>
		{{#  } }}-->
	</div>
</script>

<!-- 申请状态 -->
<script type="text/html" id="apply_state_name">
	{{#  if(d.apply_state == -1){ }}
	<span style="color: gray;">审核失败</span>
	{{#  } }}
	{{#  if(d.apply_state == 1){ }}
	<span style="color: red;">待审核</span>
	{{#  } }}
	<!--{{#  if(d.apply_state == 2){ }}
		{{#  if(d.paying_money_certificate){ }}
		<span style="color: blue;">待财务审核</span>
		{{#  }else{ }}
		<span style="color: blue;">待提交财务凭证</span>
		{{#  } }}
	{{#  } }}-->
	{{#  if(d.apply_state == 3){ }}
	<span style="color: green;">审核成功</span>
	{{#  } }}
	<!--{{#  if(d.apply_state == -2){ }}
	<span style="color: gray;">财务审核失败</span>
	{{#  } }}-->
</script>

<script type="text/html" id="site_img">
	<div class="ns-table-title">
		<!--<div class="contraction" data-id="{{d.site_id}}" data-open="0">
			<span>+</span>
		</div>
		<div class="ns-title-content">{{ d.site_name }}</div>-->
		<div class="ns-img-box img_block_sty1">
			{{#  if(d.avatar){  }}
			<img layer-src src="{{ns.img(d.avatar)}}" class="img_sty"/>
			{{#  }else{  }}
			<img layer-src src="{:img($default_img.default_city_img)}" class="img_sty"/>
			{{#  }  }}
		</div>
	</div>
</script>

<script type="text/html" id="site_name">
	<div class="ns-table-title" style="display: block;">
		<div class="ns-title-content">{{ d.shop_name }}</div>
		<div class="ns-title-content" style="color: #4e76f4;">{{ d.apply_no }}</div>
	</div>
</script>

<script type="text/html" id="apply_pass_text">
	<div class="layui-form" id="reset_pass" lay-filter="form">

		<div class="layui-form-item">
			<label class="layui-form-label mid">审核结果：</label>
			<div class="layui-input-block">
				<input type="radio" name="apply_state" value="3" title="通过" lay-filter="apply_state" checked>
				<input type="radio" name="apply_state" value="-1" title="拒绝" lay-filter="apply_state" >
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label mid"><span class="required">*</span>抽佣比例(%)：</label>
			<div class="layui-input-block">
				<input type="number" name="commission_rate" lay-verify="required" class="layui-input ns-len-mid" maxlength="18">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label mid">商家类型：</label>
			<div class="layui-input-block">
				<input type="radio" name="is_own" lay-filter="is_own" value="0" title="否" checked>
				<input type="radio" name="is_own" lay-filter="is_own" value="1" title="是" >
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label mid">备注：</label>
			<div class="layui-input-block">
				<textarea name="reason" class="layui-textarea"></textarea>
			</div>
		</div>

		<div class="ns-form-row mid">
			<button class="layui-btn ns-bg-color" lay-submit lay-filter="repass">确定</button>
			<button class="layui-btn layui-btn-primary" onclick="closePass()">返回</button>
		</div>

		<input class="reset-pass-id" type="hidden" name="member_ids" value="{{d.member_id}}"/>
	</div>
</script>
{/block}
{block name="script"}
{include file="shopapply/order_delivery_action" /}
<script>
	var element,laytpl;
	layui.use(['form', 'layer', 'laydate', 'element','laytpl'], function() {
		var table, table_website,
			laytpl = layui.laytpl,
			form = layui.form,
			layer = layui.layer,
			element = layui.element,
			laydate = layui.laydate;
		form.render();

		//渲染时间
		laydate.render({
			elem: '#start_time',
			type: 'datetime'
		});
		
		laydate.render({
			elem: '#end_time',
			type: 'datetime'
		});
		
		
		//监听Tab切换，以改变地址hash值
		element.on('tab(apply_list_tab)', function () {
			var is_addon_city = $("#is_addon_city").val();
			if (is_addon_city == 1) {
				var type = this.getAttribute('data-type');
				if (type) {
					var id = this.getAttribute('lay-id');
					$("input[name='" + type + "']").val(id);
					$("button[lay-filter='search_website']").click()
				} else {
					$("input[name='apply_state']").val("");
					$("button[lay-filter='search_website']").click()
				}
			} else {
				var type = this.getAttribute('data-type');
				if (type) {
					var id = this.getAttribute('lay-id');
					$("input[name='" + type + "']").val(id);
					$("button[lay-filter='search']").click()
				} else {
					$("input[name='apply_state']").val("");
					$("button[lay-filter='search']").click()
				}
			}
		});
		
		/**
		 * 表格加载
		 */
		table = new Table({
			elem: '#apply_list',
			url: ns.url("admin/shopapply/apply"),
			parseData: function(res) { //res 即为原始返回的数据
				return {
					"code": res.code, //解析接口状态
					"msg": res.message, //解析提示文本
					"count": res.data.count, //解析数据长度
					"data": res.data.list //解析数据列表
				};
			},
			cols: [
				[ //表头
					{
						title: '列表图片',
						width: '10%',
						unresize: 'false',
						templet: '#site_img'
					},{
						title: '网点信息',
						width: '10%',
						unresize: 'false',
						templet: '#site_name'
					}, {
						width:'10%',
						field: 'category_name',
						title: '商家分类',
						unresize: 'false',
					}, {
						title: '商家地址',
						unresize: 'false',
						width: '15%',
						templet: function (d) {
							return d.full_address + d.address;
						}
					},  {
						field: 'mobile',
						title: '商家电话',
						unresize: 'false',
						width: '8%'
					}, {
						width:'9%',
						field: 'apply_state_name',
						title: '申请状态',
						unresize: 'false',
						templet: '#apply_state_name',
					},  {
						width:'13%',
						field: 'create_time',
						title: '申请时间',
						unresize: 'false',
						templet: function(data) {
							return ns.time_to_date(data.create_time);
						},
					}, {
						width:'15%',
						title: '操作',
						toolbar: '#action',
						unresize: 'false'
					}
				]
			]
		});
		
		// 有城市分站
		table_website = new Table({
			elem: '#apply_website_list',
			url: ns.url("city://admin/shop/apply"),
			parseData: function(res) { //res 即为原始返回的数据
				return {
					"code": res.code, //解析接口状态
					"msg": res.message, //解析提示文本
					"count": res.data.count, //解析数据长度
					"data": res.data.list //解析数据列表
				};
			},
			cols: [
				[ //表头
					{
						width:'12%',
						field: 'shop_name',
						title: '店铺名称',
						unresize: 'false',
					}, {
						width:'9%',
						field: 'group_name',
						title: '开店套餐',
						unresize: 'false',
					}, {
						width:'9%',
						field: 'category_name',
						title: '主营行业',
						unresize: 'false',
					}, {
						width:'10%',
						field: 'username',
						title: '申请账号',
						unresize: 'false',
					}, {
						width:'7%',
						field: 'apply_year',
						title: '入驻时长',
						unresize: 'false',
						templet: function(data) {
							return data.apply_year+'年';
						},
					}, {
						width:'7%',
						field: 'site_area_name',
						title: '城市分站',
						unresize: 'false',
						templet: function(data) {
							return data.site_area_name == '全国' ? '--' : data.site_area_name;
						},
					}, {
						width:'10%',
						field: 'paying_amount',
						title: '<span title="入驻费用" style="padding-right: 15px;">入驻费用</span>',
						unresize: 'false',
						align: 'right',
						templet: function(data) {
							return '<span title="'+ data.paying_amount +'" style="padding-right: 15px;">￥'+ data.paying_amount +'</span>';
						},
					}, {
						width:'8%',
						field: 'apply_state_name',
						title: '申请状态',
						unresize: 'false',
						templet: '#apply_state_name',
					},  {
						width:'13%',
						field: 'create_time',
						title: '申请时间',
						unresize: 'false',
						templet: function(data) {
							return ns.time_to_date(data.create_time);
						},
					}, {
						width:'15%',
						title: '操作',
						toolbar: '#action',
						unresize: 'false'
					}
				]
			]
		});

		/**
		 * 搜索功能
		 */
		form.on('submit(search)', function(data) {
			table.reload({
				page: {
					curr: 1
				},
				where: data.field
			});
			return false;
		});
		
		// 有城市分站
		form.on('submit(search_website)', function(data) {
			table_website.reload({
				page: {
					curr: 1
				},
				where: data.field
			});
			return false;
		});
		
		/**
		 * 监听工具栏操作
		 */
		table.tool(function(obj) {
			var data = obj.data,
				event = obj.event;
			switch (event){  
				case 'apply_pass':  //审核通过
					applyPass(data);
					break;
				case 'apply_refuse':  //拒绝
					applyRefuse(data);
					break;
				case 'apply_detail':  //认证信息
					location.href = ns.url("admin/shopapply/applyDetail?apply_id=" +data.apply_id)
					break;
				case 'open_shop':  //入驻通过
					openShop(data);
					break;
				case 'edit_apply' : //修改资料
					location.href = ns.url("admin/shopapply/editApply?apply_id=" +data.apply_id)
					break;
			}
		});
		
		// 有城市分站
		table_website.tool(function(obj) {
			var data = obj.data,
				event = obj.event;
			switch (event){  
				case 'apply_pass':  //审核通过
					applyPass(data);
					break;
				case 'apply_refuse':  //拒绝
					applyRefuse(data);
					break;
				case 'apply_detail':  //认证信息
					location.href = ns.url("admin/shopapply/applyDetail?apply_id=" +data.apply_id)
					break;
				case 'open_shop':  //入驻通过
					openShop(data);
					break;
				case 'edit_apply' : //修改资料
					location.href = ns.url("admin/shopapply/editApply?apply_id=" +data.apply_id)
					break;
			}
		});
		
		// 通过审核
		function applyPass(data) {
			/*laytpl($("#apply_pass_text").html()).render(data, function(html) {
				layer_pass = layer.open({
					title: '审核',
					skin: 'layer-tips-class',
					type: 1,
					area: ['550px'],
					content: html,
				});
			});*/
			/*layer.confirm('确定要通过审核吗?', function() {
				$.ajax({
					url: ns.url("admin/shopapply/applyPass"),
					data: {
						"apply_id":data.apply_id,
					},
					dataType: 'JSON', //服务器返回json格式数据
					type: 'POST', //HTTP请求类型
					success: function(res) {
						layer.msg(res.message);
						if (res.code == 0) {
							var is_addon_city = $("#is_addon_city").val();
							if (is_addon_city == 1) {
								table_website.reload();
							} else {
								table.reload();
							}
						}
					}
				});
			});*/
		}

		form.on('submit(repass)', function(data) {

			if (repeat_flag) return false;
			repeat_flag = true;

			$.ajax({
				type: "POST",
				url: ns.url("admin/shopapply/audit"),
				data: data.field,
				dataType: 'JSON',
				success: function(res) {
					layer.msg(res.message);
					repeat_flag = false;

					if (res.code == 0) {
						layer.closeAll('page');
					}
				}
			});
		});

		function closePass() {
			layer.close(layer_pass);
		}

		// 拒绝通过
		function applyRefuse(data) {
			/**
			 * 拒绝理由弹窗
			 */
			layer.prompt({
				formType: 2,
				value: '',
				title: '请输入拒绝理由',
				area: ['300px', '100px'] ,//自定义文本域宽高
				yes: function(index, layero){
					// 获取文本框输入的值
					var value = layero.find(".layui-layer-input").val();
					value = value.replace(/\r\n/g,"");
					value = value.replace(/\s/g,"");

					if (value) {
						$.ajax({
							url: ns.url("admin/shopapply/applyReject"),
							data: {
								"apply_id":data.apply_id,
								"reason":value
							},
							dataType: 'JSON', //服务器返回json格式数据
							type: 'POST', //HTTP请求类型
							success: function(res) {
								layer.msg(res.message);
								if (res.code == 0) {
									var is_addon_city = $("#is_addon_city").val();
									if (is_addon_city == 1) {
										table_website.reload();
									} else {
										table.reload();
									}
								}
							}
						});
						layer.close(index);
					} else {
						layer.msg('请输入拒绝原因!', {icon: 5, anim: 6});
					}
				}
			}); 
		}
		
		// 入驻通过
		function openShop(data) {
			layer.confirm('确定要通过他的入驻申请吗?', function() {
				$.ajax({
					url: ns.url("admin/shopapply/openShop"),
					data: {
						"apply_id":data.apply_id,
					},
					dataType: 'JSON', //服务器返回json格式数据
					type: 'POST', //HTTP请求类型
					success: function(res) {
						layer.msg(res.message);
						if (res.code == 0) {
							var is_addon_city = $("#is_addon_city").val();
							if (is_addon_city == 1) {
								table_website.reload();
							} else {
								table.reload();
							}
						}
					}
				});
			});
		}
	});
	
	function clickAdd() {
		location.href = ns.url("admin/shopapply/addShop");
	}
</script>
{/block}